package top.lionkliu.servlet;

import lombok.extern.slf4j.Slf4j;
import top.lionkliu.entity.User;
import top.lionkliu.service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@Slf4j
@WebServlet(name = "RegisterServlet", value = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UserServiceImpl userService = new UserServiceImpl();

        String name = request.getParameter("uName");
        String pwd = request.getParameter("uPwd");
        String mail = request.getParameter("uMail");
        User user = new User(name, pwd, mail);

        Boolean isUserExist = userService.checkUser(user);
        if ( isUserExist) {
            log.warn("用户已存在！");
            response.sendRedirect("/errorPage/register_error.jsp");
        } else {
            userService.register(user);
            log.info("注册成功，用户：{}", user);
            response.sendRedirect("register_success.jsp");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
    }
}
